package com.sskj.gocoin.mine;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.AseetListBean;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.utils.APKVersionCodeUtils;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.IntentUtil;
import com.sskj.gocoin.utils.NumberUtil;
import com.sskj.gocoin.utils.NumberUtil1;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.CapitalDetailsActivity;
import com.sskj.gocoin.v.activity.ExchangeActivity;
import com.sskj.gocoin.v.activity.PayPwdSettingActivity;
import com.sskj.gocoin.v.activity.RechargeActivity;
import com.sskj.gocoin.v.activity.VerifyIdentActivity;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.gocoin.v.activity.WithdrawActivity;
import com.sskj.gocoin.v.base.BaseActivity;

import java.text.DecimalFormat;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/29
 */
public class AssetManagerActivity extends BaseActivity<AssetManagerPresenter> {

    @BindView(R2.id.toolbar)
    ToolBarLayout toolbar;
    //    @BindView(R2.id.tv_asset)
    TextView tvAsset;
    //    @BindView(R2.id.tv_asset_cny)
    TextView tvAssetCny;
    //    @BindView(R2.id.btn_recharge)
    Button btnRecharge;
    //    @BindView(R2.id.btn_withdraw)
    Button btnWithdraw;
    //    @BindView(R2.id.btn_exchange)
    Button btnExchange;
    @BindView(R2.id.asset_recyclerView)
    RecyclerView assetRecyclerView;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R2.id.rules)
    TextView rules;
    @BindView(R2.id.service)
    TextView service;
    @BindView(R2.id.version)
    TextView version;
    @BindView(R2.id.facebook)
    ImageView facebook;
    @BindView(R2.id.twitter)
    ImageView twitter;
    @BindView(R2.id.github)
    ImageView github;

    private UserInfo mUserInfo;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    DecimalFormat mFormat = new DecimalFormat("0.0000");


    @Override
    public AssetManagerPresenter getPresenter() {
        return new AssetManagerPresenter();
    }

    @Override
    public void initView() {
        initFooter();
        toolbar.setLeftButtonOnClickLinster(v -> finish());
        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        smartRefresh.setRefreshHeader(classicsHeader);
        smartRefresh.setOnRefreshListener(refreshLayout -> {
            smartRefresh.finishRefresh();
            mPresenter.upDateUserInfo(MyAppLication.getMobile());
            mPresenter.requestUserAssetList(true);
        });
        assetRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        assetRecyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setDividerColor(ContextCompat.getColor(this, R.color.fabi_1c1)));
        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(R.layout.item_asset_manage, null, assetRecyclerView) {
            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(R.id.item_asset_manage_type, item.pname);
                holder.setText(R.id.item_asset_manage_available_balance, mFormat.format(item.usable));
                holder.setText(R.id.item_asset_manage_frozen_capital, mFormat.format(item.frost));
                holder.setText(R.id.item_asset_manage_cny, NumberUtil1.keepHalf(item.cny, 2));
                holder.setOnClickListener(R.id.item_asset_manage_record, v -> {//账户明细
                    startActivity(new Intent(mActivity, CapitalDetailsActivity.class)
                            .putExtra(Constants.INTENT_ASSET_ID, item.pid)
                            .putExtra(Constants.COIN_TYPE, item.pname));
                });
                holder.setText(R.id.available_text, App.INSTANCE.getString(R.string.fabimineJavaAssetManagerActivity2) + "(" + item.pname + ")");
                holder.setText(R.id.freeze_text, App.INSTANCE.getString(R.string.fabimineJavaAssetManagerActivity3) + "(" + item.pname + ")");
            }
        };
        addHeader();
    }

    private void addHeader() {
        View headerView = LayoutInflater.from(this).inflate(R.layout.view_asset_header, null);
        tvAsset = headerView.findViewById(R.id.tv_asset);
        tvAssetCny = headerView.findViewById(R.id.tv_asset_cny);
        btnRecharge = headerView.findViewById(R.id.btn_recharge);
        btnWithdraw = headerView.findViewById(R.id.btn_withdraw);
        btnExchange = headerView.findViewById(R.id.btn_exchange);
        mAdapter.addHeaderView(headerView, 0);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_asset_manager_n;
    }

    @Override
    public void initData() {
        ClickUtil.click(btnRecharge, () -> {
            RechargeActivity.start(this, mAdapter.getData());
        });

        ClickUtil.click(btnWithdraw, () -> {
            if (!checkPwdStatus()) {
                return;
            }
            if (MyAppLication.getRealnameStatusHigh() == 3) {//已实名认证
            } else if (MyAppLication.getRealnameStatusHigh() == 2) {//审核中
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBaseActivity205));
                return;
            } else if (MyAppLication.getRealnameStatusHigh() == 4) {//被拒绝
                AlertDialog tipDialog = new AlertDialog.Builder(this)
                        .setTitle(App.INSTANCE.getString(R.string.strBaseActivity208))
                        .setMessage(App.INSTANCE.getString(R.string.mine_high_verify_tip))
                        .setPositiveButton(App.INSTANCE.getString(R.string.strBaseActivity209), (dialog, which) -> {
                            dialog.dismiss();
                            VerifyIdentActivity.start(this);
                        })
                        .setNegativeButton(App.INSTANCE.getString(R.string.strBaseActivity211), (dialog, which) -> dialog.dismiss())
                        .create();
                tipDialog.show();
                return;
            } else {
                AlertDialog tipDialog = new AlertDialog.Builder(this)
                        .setTitle(App.INSTANCE.getString(R.string.strBaseActivity208))
                        .setMessage(App.INSTANCE.getString(R.string.mine_high_verify_tip))
                        .setPositiveButton(App.INSTANCE.getString(R.string.strBaseActivity209), (dialog, which) -> {
                            dialog.dismiss();
                            VerifyIdentActivity.start(this);
                        })
                        .setNegativeButton(App.INSTANCE.getString(R.string.strBaseActivity211), (dialog, which) -> dialog.dismiss())
                        .create();
                tipDialog.show();
                return;
            }
            WithdrawActivity.start(this, mAdapter.getData());
        });
        ClickUtil.click(btnExchange, () -> {
            ExchangeActivity.start(this);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.upDateUserInfo(MyAppLication.getMobile());
        mPresenter.requestUserAssetList(true);
    }

    public void onDataSuccess(AseetListBean data) {
        mAdapter.setNewData(data.asset);
    }

    public boolean checkPwdStatus() {
        if (mUserInfo != null) {
            if (mUserInfo.tpwd != null && mUserInfo.tpwd.equals("1")) {
                return true;
            } else {
                AlertDialog dialog = new AlertDialog.Builder(this)
                        .setTitle(R.string.notice)
                        .setMessage(App.INSTANCE.getString(R.string.strTransferActivity206))
                        .setPositiveButton(getString(R.string.common_sure), (dialog1, which) -> {
                            dialog1.dismiss();
                            startActivity(new Intent(mActivity, PayPwdSettingActivity.class));
                        }).setNegativeButton(getString(R.string.common_cancel), (dialog12, which) -> dialog12.dismiss()).show();

                return false;
            }
        }
        return false;
    }

    public void updateUserInfo(UserInfo user) {
        if (user != null) {
            mUserInfo = user;
            setText(tvAsset, NumberUtil.keep4(user.getWallone() + ""));
            setText(tvAssetCny, "≈" + NumberUtil.keep2(user.getCny_money()) + "CNY");
            SPUtils.putBean(mActivity, Constants.SP_USER_INFO, user);
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AssetManagerActivity.class);
        context.startActivity(intent);
    }


    public void initFooter() {
        setText(version, App.INSTANCE.getString(R.string.fabimineJavaAssetManagerActivity8) + APKVersionCodeUtils.getVerName(this));
        ClickUtil.click(rules, () -> WebViewActivity.start(this, "trans_agree"));
        ClickUtil.click(service, () -> WebViewActivity.start(this, "reg_agree"));
        ClickUtil.click(facebook, () -> IntentUtil.browser(this,"https://www.facebook.com/GoCoiner"));
        ClickUtil.click(github, () -> IntentUtil.browser(this,"https://github.com/GoCoin"));
        ClickUtil.click(twitter, () ->IntentUtil.browser(this,"https://gocoin.com/twitter"));
    }

}
